package all.com.jinyongqunxiax.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.randy.fundation.DrawUtils;

import all.com.jinyongqunxiax.R;
import androidx.annotation.ColorInt;

public class GuideLineView extends View {
    Paint paint = new Paint();
    @ColorInt int lineColor;

    public GuideLineView(Context context) {
        super(context);
        init(null);
    }

    public GuideLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public GuideLineView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(
                attrs,
                R.styleable.GuideLineView);
        lineColor = a.getColor(R.styleable.GuideLineView_lineColor, Color.GRAY);
        a.recycle();
    }

    public static final int STEP = 8;

    @Override
    public void onDraw(Canvas canvas) {
        int right  = getWidth();
        int bottom = getHeight();

        final int step      = Math.round(STEP * DrawUtils.density(getContext()));
        final int lineWidth = Math.round(1 * DrawUtils.density(getContext()));

        if (right < lineWidth) {
            return;
        }

        //fill the background with blue color
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(0, 0, right - lineWidth, bottom, paint);


        // prepare a paint
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(lineWidth);
        paint.setAntiAlias(true);
        paint.setColor(lineColor);

        for (int i = 0; i < right; i += step) {
            canvas.drawLine(i, 0, i, bottom, paint);
        }
    }
}
